﻿var grid;
var varIncrementer = 0;
var v_PaymentLevelID = varIncrementer++,
    v_PaymentModeID = varIncrementer++,
     v_Sr = varIncrementer++,
     v_Action = varIncrementer++,
     v_PaymentModeName = varIncrementer++,
     v_PaymentLevelName = varIncrementer++,
     v_Abbriviation = varIncrementer++,
    v_Percentage = varIncrementer++,
    v_PropDurationInMonth = varIncrementer++,
    v_LevelSort = varIncrementer++,
     v_IsActive = varIncrementer++;


$(document).ready(function () {
    $('#cboStatus').select2();
    $('#txtSearchPaymentLevelName').focus();
    //fn_GetSearchResult(0);
});

function fn_GetSearchResult(p_vTrack, p_objResult) {
    if (p_vTrack == "0") {
        callBack('GetSearchResult', 'txtSearchPaymentLevelName@cboStatus');
    }
    else {
        document.getElementById("divResult").innerHTML = p_objResult.grdResult;
    }
    return false;
}

function ShowEditDiv(objState, objControl) {
    //document.getElementById("rfvtxtServiceLineName").style.display = "none";
    //document.getElementById("spnServiceLineName").style.display = "none";
    Page_ClientValidate('');

    //For Edit
    if (objState != 0) {
        $('#divInsertEdit').dialog('open');
        $('#divInsertEdit').dialog("option", "title", 'Edit Payment Level');
        var varRow = objControl.parentNode.parentNode;

        $('#hdnPaymentLevelID').val(varRow.cells[v_PaymentLevelID].innerText);
        $('#cboPaymentMode').val(varRow.cells[v_PaymentModeID].innerText);
        $('#txtPaymentLevelName').val(varRow.cells[v_PaymentLevelName].innerText);
        $('#txtAbbreviation').val(varRow.cells[v_Abbriviation].innerText);
        $('#txtPercentage').val(varRow.cells[v_Percentage].innerText);
        $('#txtPropDuration').val(varRow.cells[v_PropDurationInMonth].innerText);
        $('#txtSortOrder').val(varRow.cells[v_LevelSort].innerText); 

        $('#chkIsActive').prop('checked', (varRow.cells[v_IsActive].innerText == "Active" ? true : false));
    }
        //For Insert
    else {
        $('#divInsertEdit').dialog('open');
        $('#hdnPaymentLevelID').val("");
        $('#cboPaymentMode').val("");
        $('#txtPaymentLevelName').val("");
        $('#txtAbbreviation').val("");
        $('#txtPercentage').val("");
        $('#txtPropDuration').val("");
        $('#txtSortOrder').val("");
        $('#chkIsActive').prop('checked', false);

        $('#divInsertEdit').dialog("option", "title", 'Add Payment Level');
    }
}

function fn_SaveDetails() {

    if (Page_ClientValidate("vgSave")) {
        $('#btnSave').attr('disabled', true);
        vControlList = "cboPaymentMode@txtPaymentLevelName@txtAbbreviation@txtPercentage@txtPropDuration@txtSortOrder@chkIsActive";
        vControlList += "@txtSearchPaymentLevelName@cboStatus";

        var vAdditionalTrack = "PaymentLevelID@@" + document.getElementById('hdnPaymentLevelID').value;

        callBack('InsertEditDetails', vControlList, vAdditionalTrack);
        $('#divInsertEdit').dialog('close');
    }
    $('#btnSave').attr('disabled', false);

    return;
}

/**** Regular Expression Validator *****/
function fn_RegExpValidator(p_obj, p_args) {
    p_args.IsValid = true;
    var varCntl = $("#" + p_obj.controltovalidate);
    if (varCntl.attr("type") == "text") {
        if (varCntl.val() != "") {
            var vValidation = fn_ValidateExpression(varCntl.val(), $(p_obj).attr("expressionid"));
            if (vValidation != "") {
                args.IsValid = false;
                sender.childNodes['0'].innerHTML = vValidation;
            }
        }
    }
    return p_args.IsValid;

    //var ServiceLineName = document.getElementById('txtServiceLineName').value;
    //if (ServiceLineName != '') {
    //    var strRet = fn_ValidateExpression(ServiceLineName, '5');

    //    if (strRet != '') {
    //        var span = document.getElementById("spnServiceLineName");
    //        span.textContent = strRet;
    //        $('#spnServiceLineName').show();
    //        p_args.IsValid = false;
    //    }
    //}

}

function getResponse(returnVal) {
    try {
        hideLoader(returnVal);
        var objResult = $.parseJSON(returnVal);
        if (objResult.SessionTimeOut == "1") {
            window.location.href = 'login.aspx';
        }
        else {
            var varTrack = objResult.Track;

            if (objResult.Track == "GetSearchResult") {
                fn_GetSearchResult(1, objResult);
            }
            else if (objResult.Track == "InsertEditDetails") {
                if (objResult.IsSuccess == "1") {
                    if (parseInt($('#hdnPaymentLevelID').val()) > 0)
                        show_globalMessage(objResult.strUpdateMessage, 'info');
                    else
                        show_globalMessage(objResult.strSaveMessage, 'info');
                    fn_GetSearchResult(0);
                }

                else if (objResult.IsSuccess == "0") {
                    show_globalMessage(objResult.strMessage, 'error');
                }
                else {
                    show_globalMessage(objResult.strMessage, 'error');
                }
            }
        }
    }
    catch (ex) {
        show_globalMessage('Error', 'error');
    }
}

function fn_ExportToExcel() {//http://docs.dhtmlx.com/grid__data_export.html
    window.open('data:application/vnd.ms-excel,' + $('#divResult div').html());

    // grid.toExcel($('#hdnExportToExcelFilePath').val());
}